package com.atguigu.tingshu.user.strategy.impl;

import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.user.UserPaidAlbum;
import com.atguigu.tingshu.user.mapper.UserPaidAlbumMapper;
import com.atguigu.tingshu.user.strategy.UpdateUserPaidRecordStrategy;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class AlbumUpdateUserPaidRecord implements UpdateUserPaidRecordStrategy {

    @Autowired
    private UserPaidAlbumMapper userPaidAlbumMapper ;

    /**
     * 需要考虑接口的幂等性
     * @param userPaidRecordVo
     */
    @Override
    public void updateUserPaidRecord(UserPaidRecordVo userPaidRecordVo) {

        /**
         * 保证接口的幂等性，可以借助于数据库进行实现
         * 方案一：根据订单编号进行数据的查询，如果可以查询到数据就说明已经保存过来，就需要再次进行保存
         * 方案二：直接给订单编号的字段添加唯一索引
         */

        // 保存专辑购买信息到user_paid_album
        UserPaidAlbum userPaidAlbum = new UserPaidAlbum() ;     // 创建UserPaidAlbum对象，封装要保存的数据
        userPaidAlbum.setOrderNo(userPaidRecordVo.getOrderNo());
        userPaidAlbum.setUserId(userPaidRecordVo.getUserId());
        List<Long> itemIdList = userPaidRecordVo.getItemIdList();
        userPaidAlbum.setAlbumId(itemIdList.get(0));
        userPaidAlbumMapper.insert(userPaidAlbum) ;

    }

    @Override
    public String getItemType() {
        return SystemConstant.ORDER_ITEM_TYPE_ALBUM;    // 1001
    }

}
